<?php
/***********************************************************
 * Base User Controller
 ***********************************************************/

require_once 'Zend/Session/Namespace.php';
require_once 'CMS/Mvc/BaseController.php';

abstract class BaseUserController extends BaseController
{
	
	public function init()
	{
		parent::init();
		//$this->messageManager->LoadMessages(APPLICATION_PATH . $path);
		
		$this->isLoggedin();
	}
	
	/**
	 * checking the user is logged in.
	 * if not, redirect to ...
	 */
	public function isLoggedin()
	{
		$session = $this->sessionCreate(LOGINID_NAMESPACE);

		if(!isset($session->loginID)){
			$this->view->LoggedIn = false;
			return false;
		}

		$this->view->LoggedIn = true;
		return true;
	}
	
	
	/**
	 * session object creator
	*/
	public function sessionCreate($name)
	{
		return new Zend_Session_Namespace($name);
	}

	/**
	 * session login id creator
	*/
	public function sessionLoginIdCreate($id)
	{
		$sessionLogin = new Zend_Session_Namespace(LOGINID_NAMESPACE);
		$sessionLogin->loginID = $id;
		$sessionLogin->setExpirationSeconds(1800);
	}

	
	
	/**
	 * session login id destroyer
	*/
	public function sessionLoginIdDestroy()
	{
		try {
			Zend_Session::namespaceUnset(LOGINID_NAMESPACE);
			
		} catch (Exception $e) {
			return false;
		}
	}

	
	
	/**
	 * session object destroyer
	*/
	public function sessionDestroy($name)
	{
		try {
			Zend_Session::namespaceUnset($name);
			
		} catch (Exception $e) {
			echo $e;	
		}
	}
	
	/**
	 * get the path of referer
	*/

	public function getPath()
	{
		$session = $this->sessionCreate();
		$url     = parse_url($_SERVER['HTTP_REFERER']);
		$path    = $url['path'];

		$session->path = $path;
		return $path;
	}

	/**
	 * redirect to top
	*/

	public function redirectTop(){
		$this->_redirect(DIRECT_ROOT);
	}

	/**
	 * check the authority and if not authorized, redirect to top
	 */

	public function redirectLogin(){
		$this->_redirect(DIRECT_LOGIN);
	}


	/**
	 * get loggedin user ID from session
	 */

	public function getSessionUser(){
		$user_id = $this->sessionCreate(LOGINID_NAMESPACE)->loginID;
		return $user_id;
	}

	
}